package cn.mdx.common.utils;

import cn.mdx.common.exceptions.CheckException;

import java.lang.reflect.Field;
import java.util.Arrays;

public class CheckUtil {
    public static void check(boolean condition, String msg) {
        if (!condition) {
            fail(msg);
        }
    }


    public static void mustEmpty(String str, String msg) {
        if (str != null && (!str.trim().isEmpty())) {
            fail(msg);
        }
    }

    public static void mustNull(Object obj, String msg) {
        if (obj != null) {
            fail(msg);
        }
    }


    public static void notEmpty(String str, String msg) {
        if (str == null || str.trim().isEmpty()) {
            fail(msg);
        }
    }

    public static void notNull(Object obj, String msg) {
        if (obj == null) {
            fail(msg);
        }
    }

    public static void fail(String msg) {
        throw new CheckException(msg);
    }


    //数组中的属性名必为不空，其他随意
    public static void notNull(Object obj,String msg,String arg)  {
        notNull(obj,msg);
        String[] args = arg.split("&");
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields){
            field.setAccessible(true);
            String name = field.getName();
            Object value = null;
            try {
                value = field.get(obj);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            String type = field.getGenericType().toString();
            if(Arrays.asList(args).contains(name)) {
                if(type.equals("class java.lang.String")){
                    notEmpty((String)value,msg);
                } else {
                    notNull(value,msg);
                }

            }
        }
    }

    //数组中的属性名必为不空，其他必为空
    public static void notNullOtherOpposite(Object obj,String msg,String arg)  {
        notNull(obj,msg);
        String[] args = arg.split("&");
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields){
            field.setAccessible(true);
            String name = field.getName();
            Object value = null;
            try {
                value = field.get(obj);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            String type = field.getGenericType().toString();
            if(Arrays.asList(args).contains(name)) {
                if(type.equals("class java.lang.String")){
                    notEmpty((String)value,msg);
                } else {
                    notNull(value,msg);
                }

            } else {
                if(type.equals("class java.lang.String")){
                    mustEmpty((String)value,msg);
                } else {
                    mustNull(value,msg);
                }
            }
        }
    }

    //数组中的属性名必为空，其他必为不空
    public static void mustNullOtherOpposite(Object obj,String msg,String arg)  {
        notNull(obj,msg);
        String[] args = arg.split("&");
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields){
            field.setAccessible(true);
            String name = field.getName();
            Object value = null;
            try {
                value = field.get(obj);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            String type = field.getGenericType().toString();
            if(Arrays.asList(args).contains(name)) {
                if(type.equals("class java.lang.String")){
                    mustEmpty((String)value,msg);
                } else {
                    mustNull(value,msg);
                }
            } else {
                if(type.equals("class java.lang.String")){
                    notEmpty((String)value,msg);
                } else {
                    notNull(value,msg);
                }
            }
        }
    }

    //数组中的属性名必为空，其他随意
    public static void mustNull(Object obj,String msg,String arg)  {
        notNull(obj,msg);
        String[] args = arg.split("&");
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields){
            field.setAccessible(true);
            String name = field.getName();
            Object value = null;
            try {
                value = field.get(obj);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            String type = field.getGenericType().toString();
            if(Arrays.asList(args).contains(name)) {
                if(type.equals("class java.lang.String")){
                    mustEmpty((String)value,msg);
                } else {
                    mustNull(value,msg);
                }
            }
        }
    }





    /**
     * 验证是否为正确的邮箱号
     *
     * @param email
     * @return
     */
    public static void mustEmail(String email,String msg) {


    }


}
